<?php
namespace app\common\model;

use think\Model;

class _Base extends Model
{
    // 状态 禁用 启用 删除
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;
    const STATUS_DELETED = -1;

    // 显示状态
    const STATUS_SHOW = [
        self::STATUS_ENABLE,
        self::STATUS_DISABLE,
    ];

    // 全部状态
    const STATUS_ALL = [
        self::STATUS_ENABLE,
        self::STATUS_DISABLE,
        self::STATUS_DELETED,
    ];

    const TYPE_USER = 2;
    const TYPE_ADMIN = 1;

    const USER_TYPE = [
        self::TYPE_USER,
        self::TYPE_ADMIN,
    ];

    // 类型转换
    protected $type = [
        'status'    => 'integer',
        'id'        => 'integer',
        'uid'       => 'integer',
    ];

    // 状态转文字
    public static function getStatusTextAttr($value,$data)
    {   
        switch ($data['status']) {
            case self::STATUS_ENABLE:
                $status = '正常';
                break;
            case self::STATUS_DISABLE:
                $status = '禁用';
                break;
            case self::STATUS_DELETED:
                $status = '已删除';
                break;
        }
        return $status;
    }

    // 根据status值 返回cond
    public static function getStatusCond($data, $alias = false)
    {
        $alias = $alias?$alias.'.':'';
        if( !isset($data['status']) || strlen($data['status']) == 0 || $data['status'] == 2 ){
            $cond[] = [$alias.'status','in',self::STATUS_SHOW];
        }else{
            $cond[] = [$alias.'status','in',$data['status']];
        }
        return $cond;
    }

    // 统一时间转换
    public static function getTime($value)
    {
        return $value?date(config('database.datetime_format'),$value):'';
    }


}